<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #app {
            width: 600px;
            margin: 50px auto;
        }

        #app .item {
            float: left;
            width: 100px;
            height: 40px;
            background-color: black;
            color: cadetblue;
            text-align: center;
            line-height: 40px;
        }

        #app .item input {
            width: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <my-header :checkedall="checkedAll" @setcheckedall="setCheckedAll"></my-header>
        <my-list :list="list" @del="del"></my-list>
        <my-footer :num="totalnum" :price="totalprice" @delall="delAll"></my-footer>
    </div>
</body>
<template id="myheader">
    <div class="header">
        <div class="item">
            <input type="checkbox" :checked="checkedall" @change="setCheckedAll">全选
        </div>
        <div class="item">商品名称</div>
        <div class="item">商品价格</div>
        <div class="item">商品数量</div>
        <div class="item">商品小计</div>
        <div class="item">操作</div>
    </div>
</template>
<template id="mylist">
    <div>
        <div class="tr" v-for="item in  list">
            <div class="item">
                <input type="checkbox" v-model="item.checked">
            </div>
            <div class="item">{{item.goodsname}}</div>
            <div class="item">{{item.goodsprice}}</div>
            <div class="item">

                <button v-show="item.goodsnum>1" @click="calc(item,-1)">-</button>
                <input v-model="item.goodsnum" />
                <button @click="calc(item,1)">+</button>

            </div>
            <div class="item">{{item.goodsnum*item.goodsprice}}</div>
            <div class="item" @click="del(item.id)">删除</div>
        </div>
    </div>

</template>

<template id="myfooter">
    <div class="footer">
        <div class="item" @click="delAll">
            批量删除
        </div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item">{{num}}</div>
        <div class="item">{{price}}</div>
        <div class="item"></div>
    </div>
</template>
<script>
    let myHeader = {
        props: ["checkedall"],
        template: "#myheader",
        methods: {
            setCheckedAll(e) {
                this.$emit("setcheckedall", e.target.checked);
            }
        }
    }
    let  myList = {
        props:["list"],
        template:"#mylist",
        methods:{
            calc(item,num){
                item.goodsnum  =item.goodsnum +num;
            },
            del(id){
                this.$emit("del",id);
            }
        }
    }
    let  myFooter = {
        props:["num","price"],
        template:"#myfooter",
        methods:{
            delAll(){
                this.$emit("delall");
            }
        }
    }
    new Vue({
        el: "#app",
        data: {
            checkedAll: false,
            list: [{ id: 1, goodsname: "商品一", goodsnum: 1, goodsprice: 100, checked: false }, { id: 2, goodsname: "商品二", goodsnum: 2, goodsprice: 100, checked: false }, { id: 3, goodsname: "商品三", goodsnum: 3, goodsprice: 100, checked: false }]
        },
        components: {
            myHeader,
            myList,
            myFooter
        },
        watch: {
            list: {
                deep: true,
                handler(newList) {
                    if (newList.length) {
                        this.checkedAll = newList.every(item => item.checked);
                    } else {
                        this.checkedAll = false;
                    }

                }
            }

        },
        methods: {
        
            del(id) {
                this.list = this.list.filter(item => item.id != id);
            },
            delAll() {
                this.list = this.list.filter(item => !item.checked);
            },
            setCheckedAll(flag) {
                this.list.forEach(item => {
                    item.checked = flag;
                })
            }
        },
        computed: {
            totalnum() {
                let selectList = this.list.filter(item => item.checked);
                if (selectList.length) {
                    return selectList.map(item => item.goodsnum).reduce((a, b) => a + b);
                }
                return 0;
            },
            totalprice() {
                let selectList = this.list.filter(item => item.checked);
                if (selectList.length) {
                    return selectList.map(item => item.goodsnum * item.goodsprice).reduce((a, b) => a + b);
                }
                return 0;
            }
        }
    })
</script>

</html>